/*
 * Copyright (C), 2002-2017, 南京苏宁软件技术有限公司
 * FileName: ExchangeActivityDaoImpl.java
 * Author:   10070706
 * Date:     2017年5月4日 下午2:46:05
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.service.impl.dao.exchange;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.google.common.collect.Maps;
import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.intf.dao.exchange.ExchangeActivityDao;
import com.suning.sawp.po.exchange.ExchangeActivity;
import com.suning.sawp.po.exchange.ExchangeActivityRule;
import com.suning.sawp.service.util.BeanUtil;
import com.suning.sawp.vo.exchange.ActivityRuleVo;

/**
 * 〈以旧换新活动表dao实现〉<br>
 *
 * @author 10070706
 */
@Repository
public class ExchangeActivityDaoImpl implements ExchangeActivityDao {

	@Resource(name = "dalClient")
	private DalClient dalClient;
	@Resource(name = "dalClientRead")
	private DalClient dalClientRead;

	@Override
	public List<String> queryExchangeActivity(List<String> activityIds) {
		Map<String, Object> param = Maps.newHashMap();
		param.put("activityIds", activityIds);
		return dalClientRead.queryForList("exchange.activity.queryExchangeActivity", param, String.class);
	}

	@Override
	public List<ActivityRuleVo> queryActivityRule(List<String> activityIds) {
		Map<String, Object> param = Maps.newHashMap();
		param.put("activityIds", activityIds);
		return dalClientRead.queryForList("exchange.activity.queryActivityRule", param, ActivityRuleVo.class);
	}

	@Override
	public List<ExchangeActivity> queryExchangeActivityInfo(List<String> activityIds) {
		Map<String, Object> param = Maps.newHashMap();
		param.put("activityIds", activityIds);
		return dalClientRead.queryForList("exchange.activity.queryExchangeActivityInfo", param, ExchangeActivity.class);
	}

	public List<ExchangeActivity> queryActivityList() {
		return dalClientRead.queryForList("exchange.activity.queryActivityList", null, ExchangeActivity.class);
	}

	public void saveActivity(ExchangeActivity activity) {
		dalClient.execute("exchange.activity.saveActivity", activity);
	}

	public boolean batchSaveActivityRule(List<ExchangeActivityRule> list) {
		if (CollectionUtils.isEmpty(list)) {
			return false;
		}
		@SuppressWarnings("unchecked")
		Map<String, Object>[] params = new Map[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = BeanUtil.describe(list.get(i));
		}
		int[] result = dalClient.batchUpdate("exchange.activity.saveActivityRule", params);
		if (result == null || result.length == 0) {
			return false;
		}
		int successNum = 0;
		for (int r : result) {
			successNum += r;
		}
		return successNum == list.size();
	}

	public void delActivityRulesByActivityId(String activityId) {
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("activityId", activityId);
		dalClient.execute("exchange.activity.delActivityRulesByActivityId", paramMap);
	}

	public ExchangeActivity findActivityById(String activityId) {
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("activityId", activityId);
		return dalClientRead.queryForObject("exchange.activity.findActivityById", paramMap, ExchangeActivity.class);
	}

	public List<ExchangeActivityRule> queryActivityRulesByActivityId(String activityId) {
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("activityId", activityId);
		return dalClientRead.queryForList("exchange.activity.queryActivityRulesByActivityId", paramMap, ExchangeActivityRule.class);
	}

}
